/*
* pps_bom
*/
package com.pitop.project.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.project.entity.ProjectCategory;
import com.pitop.project.service.ProjectCategorySVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目分类 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/projectCategory")
@Slf4j
@Api(value = "项目分类控制器", description = "项目分类控制器")
public class ProjectCategoryCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private ProjectCategorySVImpl projectCategorySVImpl;


    /**
     * 根据条件id查询ProjectCategory一个详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProjectCategory一个详情信息", notes = "查询ProjectCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        ProjectCategory projectCategory = projectCategorySVImpl.loadById(id);
        log.info(JSON.toJSONString(projectCategory));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, projectCategory);
    }

    /**
     * 根据条件code查询ProjectCategory一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询ProjectCategory一个详情信息", notes = "查询ProjectCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        ProjectCategory projectCategory = projectCategorySVImpl.loadByCode(code);
        log.info(JSON.toJSONString(projectCategory));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, projectCategory);
    }


    /**
     * 查询ProjectCategory信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询ProjectCategory信息集合", notes = "查询ProjectCategory信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore ProjectCategory projectCategory, @ApiIgnore Page<ProjectCategory> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<ProjectCategory> projectCategorys = projectCategorySVImpl.list(projectCategory, page.getCurPage(), page.getPageSize());
        int total = projectCategorySVImpl.count(projectCategory);
        page.setTotalRow(total);
        page.setVoList(projectCategorys);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建ProjectCategory
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建ProjectCategory", notes = "创建ProjectCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "分类名称,最大32个汉字，64个英文", paramType = "query"),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore ProjectCategory projectCategory) {
        if (StringUtils.isEmpty(projectCategory.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(projectCategory.getEnglishName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        projectCategorySVImpl.save(projectCategory);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, projectCategory);
    }

    /**
     * 修改ProjectCategory
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改ProjectCategory", notes = "修改ProjectCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "分类名称,最大32个汉字，64个英文", paramType = "query"),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore ProjectCategory projectCategory) {
        if (projectCategory.getId() == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(projectCategory.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        projectCategorySVImpl.modify(projectCategory);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, projectCategory);
    }

    /**
     * 根据主键删除ProjectCategory
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除ProjectCategory", notes = "删除ProjectCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        if (id == null && StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        projectCategorySVImpl.delete(id, code);
        return BeanRet.create(true, "删除ProjectCategory成功");
    }

}
